<template>
  <div>
    <el-form :inline="true" label-width="100%">
      <el-form-item>
        <el-input
          v-model="keyword"
          placeholder="请输入搜索内容"
          style="width: 400px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="info" style="margin-right: 20px" @click="getuserlist"
          >搜索</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="dialogFormVisible = true"
          >添加用户</el-button
        >
      </el-form-item>
    </el-form>

    <el-table :data="goods" style="width: 100%" border>
      <el-table-column label="会员" width="'180'">
        <template slot-scope="scope">
          <div class="byc1">
            <img :src="scope.row.avatar" alt="" />
            <div class="bycright">
              <p>{{ scope.row.username }}</p>
              <p>{{ scope.row.id }}</p>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="会员等级" width="'180'">
        <template slot-scope="scope">
          {{ scope.row.user_level.name }}
        </template>
      </el-table-column>
      <el-table-column label="登陆注册" width="'180'">
        <template slot-scope="scope">
          {{ scope.row.create_time }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="'180'">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.status == 0">禁用</el-tag>
          <el-tag type="success" v-else>启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="'180'">
        <template slot-scope="scope">
          <el-button type="primary" :id="scope.row.id">修改</el-button>
          <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="formName">
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像" :label-width="formLabelWidth">
          <el-input v-model="form.avatar" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员等级id" :label-width="formLabelWidth">
          <el-select v-model="form.user_level_id" autocomplete="off" placeholder="请选择">
            <el-option ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" :label-width="formLabelWidth">
          <el-input v-model="form.status" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="adduser('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: "demo",
  // 组件参数 接收来自父组件的数据
  props: [],
  // 局部注册的组件
  components: {},
  // 组件状态值
  data() {
    return {
      limit: 3,
      page: 1,
      total: 0,
      keyword: "",
      goods: [],
      dialogFormVisible: false,
      formLabelWidth: "80px",

      form: {
        username: "",
        password: "",
        nickname: "",
        avatar: "",
        user_level_id: "",
        phone: "",
        email: "",
        status: "",
      },
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    getuserlist() {
      this.$axios
        .get(
          `/api/user/${this.page}?limit=${this.limit}&keyword=${this.keyword}&user_level_id`
        )
        .then((res) => {
          console.log(res);
          this.goods = res.data.list;
          this.total = res.data.totalCount;
        });
    },
    handleSizeChange(size) {
      this.limit = size;
      this.getuserlist();
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getuserlist();
    },
    // 添加用户
    adduser() {
      this.$axios.post("/api/user", this.form),
        then((res) => {
          console.log(res);
        });
    },
    // 删除
    del(id) {
      this.$axios.post(`/api/user/:id=${id}/delete`).then((res) => {
        console.log(res);
      });
    },
  },
  //生命周期
  created() {},
  mounted() {
    this.getuserlist();
  },
};
</script> 

<style scoped lang="scss">
.byc1 {
  display: flex;
  justify-content: space-between;
  img {
    width: 100px;
    height: 120px;
  }
}
.el-pagination {
  margin-top: 20px;
}
.el-table {
  margin-top: 20px;
}
.el-form {
  font-size: 18px;
}
</style>
